/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package piplayer.impl.common;

import java.io.File;

import java.nio.file.Path;
import java.nio.file.FileSystems;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author richb
 */
public class Library2
{
    
    public class DirFuncs
    {
        
        public Path getParent(Path dir)
        {
            
            if (dir.equals(Library2.this._rootDir))
            {
                /* cannot go deeper */
                throw new Error("dir is already the library root node");
            }
            else
            {
                final Path parent = dir.getParent();
                return parent;
            }
            
        }
        
        public List<Path> getDirectories(Path dir)
        {
            
            final List<File> files1 = Arrays.asList(dir.toFile().listFiles());
            
            final List<File> dirs1 = files1.stream().filter((File f) -> f.isDirectory()).collect(Collectors.toList());            
            
            final List<Path> paths1 = dirs1.stream().map((File f) -> f.toPath()).collect(Collectors.toList());
            
            return paths1;
            
        }
        
        public Path getNext(Path dir)
        {
            
            final List<Path> parentsDirs = this.getDirectories(this.getParent(dir));
            
            final int indexCurrent = parentsDirs.indexOf(dir);
            
            final int returnIndex = (indexCurrent + 1) % parentsDirs.size();
            
            return parentsDirs.get(returnIndex);
            
        }
        
        public Path getPrevious(Path dir)
        {
            final List<Path> parentsDirs = this.getDirectories(this.getParent(dir));
            
            final int indexCurrent = parentsDirs.indexOf(dir);
            
            final int returnIndex = (indexCurrent - 1) < 0 ? parentsDirs.size() - 1 : (indexCurrent - 1);
            
            return parentsDirs.get(returnIndex);
        }
        
        public Path getFirstContainedDir(Path dir)
        {
            return this.getDirectories(dir).get(0);
        }
        
    }
    
    public class FileFuncs
    {
        
    }
    
    private final DirFuncs _dirFuncs = new DirFuncs();
    private final FileFuncs _fileFuncs = new FileFuncs();
    private final Path _rootDir;
    
    public Library2(String rootDir)
    {
        _rootDir = FileSystems.getDefault().getPath(rootDir);;
    }
    
    public DirFuncs dir()
    {
        return _dirFuncs;
    }
    
    public FileFuncs file()
    {
        return _fileFuncs;
    }
    
    public Path getLibraryRoot()
    {
        return this._rootDir;
    }

}
